// import {getCurrentInstance} from "vue";
import {my_person_list} from "@/utils/common_data.js";

let socket1 = null;

export const connect = (proxy)=> {
    socket1 = new WebSocket("ws://129.0.0.65:8000/room/111/");
    // const { proxy } = getCurrentInstance()
    // 连接完成以后 客户端自动触发
    socket1.onopen = function (event) {
        let tag = document.createElement("div")
        tag.innerHTML = "连接已建立"
        document.getElementById("message1").appendChild(tag)
    }

    // 回调函数 当服务端有消息发送到时，自动触发
    socket1.onmessage = function (event) {
        console.log("event.data", event.data);
        proxy.$bus.emit("change_flag", event.data)
        if (my_person_list.value.includes(event.data)){
            proxy.$bus.emit("change_flag", event.data)
        }else {
            let tag = document.createElement("div");
            tag.innerText = event.data;
            let msg = document.getElementById("message1")
            msg.appendChild(tag)
            msg.scrollTop = msg.scrollHeight
        }
    }

    // 当连接关闭时，触发
    socket1.onclose = function (event) {
        let tag = document.createElement("div")
        tag.innerHTML = "连接已断开"
        document.getElementById("message1").appendChild(tag)

    }
}
export const sendMsg = ()=> {
    let tag = document.getElementById("txt")
    socket1.send(tag.value)
}
export const closeConn = ()=> {
    // 向服务端发送断开连接
    socket1.close();
}